import MySQLHelper as DB
import os
import time


if __name__ == '__main__':
    DB.MySQL_Init()  # 初始化连接
    DB.check_data()  # 检查是否需要更新data表内容
    events = DB.get_EVENTS()  # 获取今日列表
    while True:
        os.system('cls')
        print('今日打卡列表:')
        print('未打卡:')
        count = 0
        for i in events.keys():
            if events[i].mode == 0 and events[i].checked == 0:
                print('\t{}.{}'.format(events[i].event_id, events[i].name))
                count += 1
            if count == 0:
                print('\t无')
        count = 0
        print('已打卡:')
        for i in events.keys():
            if events[i].mode == 0 and events[i].checked == 1:
                print('\t{}.{}'.format(events[i].event_id, events[i].name))
                count += 1
            if count == 0:
                print('\t无')
        count = 0
        print('正打卡:')
        for i in events.keys():
            if events[i].mode == 1 and events[i].checked == 1:
                print('\t{}.{}'.format(events[i].event_id, events[i].name))
                count += 1
            if count == 0:
                print('\t无')
        count = 0
        print('打卡失败:')
        for i in events.keys():
            if events[i].mode == 1 and events[i].checked == 0:
                print('\t{}.{}'.format(events[i].event_id, events[i].name))
                count += 1
            if count == 0:
                print('\t无')

        cmd = input('\n\n您想要:\n'
                    '1.更改打卡状态\n'
                    '2.新增打卡项\n'
                    '3.删除打卡项\n'
                    '4.查看日历\n'
                    'q.退出\n')
        if cmd == '1':
            event_id = input('请输入事件代号:')
            try:
                event_id = int(event_id)
            except TypeError:
                print('错误:错误类型,请输入数值!')
                time.sleep(3)
                continue
            else:
                if event_id not in events.keys():
                    print('错误:编号未找到！')
                    time.sleep(3)
                    continue

            checked = 'none'
            if events[event_id].mode == 0:
                checked = input('请输入打卡状态,0未打卡 1已打卡:')
            elif events[event_id].mode == 1:
                checked = input('请输入打卡状态,0打卡失败 1正打卡:')
            try:
                checked = int(checked)
            except TypeError:
                print('错误:错误类型,请输入数值!')
                time.sleep(3)
                continue
            else:
                if checked < 0 or checked > 1:
                    print('错误:输入超出范围!')
                    time.sleep(3)
                    continue

            note = input('请输入备注:')
            if len(note) > 255:
                print('错误:备注过长!')
                time.sleep(3)
                continue

            events[event_id].change_data(checked, note)
        elif cmd == '2':
            name = input('事务名称:')
            if len(name) >= 255:
                print('错误:备注过长!')
                time.sleep(3)
                continue

            mode = input('模式,0普通 1自动打卡手动撤销:')
            if mode == '0':
                mode = 0
            elif mode == '1':
                mode = 1
            else:
                print('错误:未知模式!')
                time.sleep(3)
                continue

            DB.add_event(name, mode)
            DB.check_data()
            events = DB.get_EVENTS()
        elif cmd == '3':
            event_id = input('请输入事务id:')
            try:
                event_id = int(event_id)
            except TypeError:
                print('错误:错误类型,请输入数值!')
                time.sleep(3)
                continue
            else:
                if event_id not in events.keys():
                    print('错误:未找到该id!')
                    time.sleep(3)
                    continue

            DB.del_event(event_id)
            events = DB.get_EVENTS()
        elif cmd == '4':
            print('呃，不好意思，还没写')
        elif cmd == 'q':
            break
    DB.close_DB()
